package com.forlove.app.qselect.login.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.login.entity.LoginEntity;
import com.forlove.app.qselect.login.entity.RegisterEntity;
import com.forlove.app.qselect.login.entity.UserEntity;
import com.forlove.app.qselect.login.service.IRegisterPhoneService;
import com.forlove.app.qselect.login.service.impl.RegisterPhoneServiceImpl;
import com.forlove.app.qselect.login.view.IRegisterPhoneView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yjl on 2017/10/16.
 */

public class RegisterPhonePresenter extends IBasePresenter {
    public final int RESULT_SUCCESS = 1;
    public final int RESULT_FAIL = 0;
    private IRegisterPhoneService registerPhoneService;
    private IRegisterPhoneView registerPhoneView;

    public RegisterPhonePresenter(IRegisterPhoneView iRegisterPhoneView) {
        if (registerPhoneService == null) {
            registerPhoneService = new RegisterPhoneServiceImpl();
            setService(registerPhoneService);
        }
        this.registerPhoneView = iRegisterPhoneView;
    }

    /**
     * 请求手机号
     */
    public void loadPhoneData(String phoneCode, String sign) {
        Map<String, String> param = new HashMap<>();
        param.put("mobile", phoneCode);
        param.put("sign", sign);
        registerPhoneService.loadData(ServerManager.ACTION_REG_FIRST, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "success:" + result);
                if (result != null) {
                    CommonEntity commonEntity = new CommonEntity();
                    commonEntity.parseJSONString(result);
                    RegisterEntity registerEntity = ParseHelper.getModel(result, "data", RegisterEntity.class);
                    if (registerPhoneView != null) {
                        registerPhoneView.showRequestPhoneView(registerEntity, "");
                    }
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                LogUtil.e("yjl", "onfail:" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    registerPhoneView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    registerPhoneView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "请稍后重试!");
                }

            }
        });
    }

    /**
     * 验证验证码
     */
    public void loadIdentifyCode(Map param) {
        if (param == null) {
            return;
        }
        registerPhoneService.loadData(ServerManager.ACTION_REG_STEP1, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "success result:" + result);
//                CommonEntity commonEntity = new CommonEntity();
//                if (result != null) {
//                    commonEntity.parseJSONString(result.toString());
//                }
                LoginEntity loginEntity = ParseHelper.getModel(result, "data", LoginEntity.class);
                if (loginEntity != null) {
                    registerPhoneView.showIdentifyCodeView(
                            NetworkManager.ResultCode.RESULT_SUCCESS.getValue(), loginEntity);
                }

            }

            @Override
            public void onFail(int statusCode, String result) {
                LogUtil.e("yjl", "onfail:" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    registerPhoneView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    if (registerPhoneView != null) {
                        registerPhoneView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }


    public void loadSetPWData(final LoginEntity loginEntity) {
        Map<String, String> param = new HashMap<>();
        param.put("mobile", loginEntity.mobile);
        param.put("valid_token", loginEntity.valid_token);
        param.put("mobile_type", loginEntity.mobile_type);
        param.put("device_no", loginEntity.device_no);
        param.put("location", loginEntity.location);
        param.put("password", loginEntity.password);
        registerPhoneService.loadData(ServerManager.ACTION_REG_STEP2, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                UserEntity userEntity = new UserEntity();
                userEntity.parseJSONString(ParseHelper.getString(result, "data"));
                userEntity.password = loginEntity.password;
                userEntity.lastloc = loginEntity.location;
                userEntity.device_no = loginEntity.device_no;
                if (registerPhoneView != null) {
                    registerPhoneView.showSetPassWordView(NetworkManager.ResultCode.RESULT_SUCCESS.getValue(), userEntity, "");
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                String errorMsg = "请稍后重试！";
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    registerPhoneView.showSetPassWordView(
                            NetworkManager.ResultCode.RESULT_FAIL.getValue(), null, commonEntity.message);
                } else {
                    registerPhoneView.showSetPassWordView(
                            NetworkManager.ResultCode.RESULT_FAIL.getValue(), null, errorMsg);
                }
            }

        });
    }


    public void loadUpJPushRegisterId() {
        registerPhoneService.loadData(ServerManager.ACTION_UP_REGISTERID, new HashMap<>(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "上传jpush id:" + result);
            }

            @Override
            public void onFail(int statusCode, String result) {
                LogUtil.e("yjl", "上传jpush id:" + result);
            }
        });
    }
}
